package be.sc.classicnetwork.dao.hibernate;

import java.util.Collection;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import be.sc.classicnetwork.dao.CodeDao;
import be.sc.classicnetwork.model.Code;

/**
 * CodeDaoImpl.
 */
public class CodeDaoImpl extends HibernateDaoSupport implements CodeDao {

	/**
	 * @see be.sc.classicnetwork.dao.CodeDao#findAll(java.lang.Class)
	 */
	@SuppressWarnings("unchecked")
	public Collection<? extends Code> findAll(Class<? extends Code> codeClass) {
		DetachedCriteria criteria = DetachedCriteria.forClass(codeClass);
		criteria.addOrder(Order.asc("name"));
		return getHibernateTemplate().findByCriteria(criteria);
	}

	/**
	 * @see be.sc.classicnetwork.dao.CodeDao#findById(java.lang.Class, int)
	 */
	@SuppressWarnings("unchecked")
	public Code findById(Class<? extends Code> codeClass, int id) {
		return (Code) getHibernateTemplate().get(codeClass, id);
	}

	/**
	 * @see be.sc.classicnetwork.dao.CodeDao#findByIdIn(java.lang.Class,
	 *      java.lang.Integer[])
	 */
	@SuppressWarnings("unchecked")
	public Collection<? extends Code> findByIdIn(
			Class<? extends Code> codeClass, String... ids) {
		DetachedCriteria criteria = DetachedCriteria.forClass(codeClass);
		criteria.add(Restrictions.in("id", ids));
		return getHibernateTemplate().findByCriteria(criteria);
	}

}
